<?php
// -----------------------------------------------------------------------------
// include
// -----------------------------------------------------------------------------

include "../config/config.php";
include "../auth.php";
include "../DBManager.php";
include "../vendor/autoload.php";

use Overtrue\ChineseCalendar\Calendar;

// -----------------------------------------------------------------------------
// 对象初始化
// -----------------------------------------------------------------------------

$calendar = new Calendar();

// -----------------------------------------------------------------------------
// 数据库相关设置
// -----------------------------------------------------------------------------

$dbPath = "../" . $dbPath; // 不加这行语句的话会报错，提示找不到对应路径
$db = new DBManager("sqlite:" . $dbPath);

// -----------------------------------------------------------------------------
?>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <title><?php echo $linkTitleViewRepeatTasks; ?></title>
    <base target="_self">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="../resources/bootstrap-5.2.0-beta1-dist/css/bootstrap.min.css">
</head>
<body>

<!-- ----------------------------------------------------------------------- -->

<script src="../resources/bootstrap-5.2.0-beta1-dist/js/bootstrap.min.js"></script>
<script src="../resources/jQuery/jquery-3.5.1.js"></script>
<?php include "../globalNavbar.php"; ?>

<!-- ----------------------------------------------------------------------- -->

<!--div:cintainer start-->
<div class="container container-fluid mb-5">

    <!-- 导航栏 开始 -->
    <div class="mt-3">
        <nav style="--bs-breadcrumb-divider: '>';" aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a class="text-decoration-none" href="<?php echo $homepage; ?>">首页</a></li>
                <li class="breadcrumb-item active"><?php echo $linkTitleViewRepeatTasks; ?></li>
            </ol>
        </nav>
    </div>
    <!-- 导航栏 结束 -->

    <div class="mt-2 mb-3">

        <div class="mt-0">
            <h3>每日重复</h3>
            <div class="list-group">
                <a href="../edit.php?from=daily" class="list-group-item list-group-item-action">每日</a>
            </div>
        </div>

        <?php
        $dayOfWeeksWithTask = $db->getListOfDayOfWeekWithTask();
        $dayOfMonthsWithTask = $db->getListOfDayOfMonthWithTask();
        $dayOfMonthsLunarWithTask = $db->getListOfDayOfMonthLunarWithTask();
        $monthAndDaysWithTask = $db->getListOfMonthAndDayWithTask();
        $monthAndDaysLunarWithTask = $db->getListOfMonthAndDayLunarWithTask();
        ?>

        <?php if (count($dayOfWeeksWithTask) !== 0): ?>

            <div class="mt-2">
                <h3>每周重复</h3>
                <div class="list-group mt-2">
                    <?php
                    $days = ["日", "一", "二", "三", "四", "五", "六"];
                    foreach ($dayOfWeeksWithTask as $dayOfWeek) {
                        $week = $days[$dayOfWeek];
                        echo <<<BBB
                            <a href="../edit.php?from=weekly&dayOfWeek={$dayOfWeek}" class="list-group-item list-group-item-action">每周{$week}</a>
                        BBB;
                    }
                    ?>
                </div>
            </div>

        <?php endif; ?>

        <?php if (count($dayOfMonthsWithTask) !== 0): ?>

            <div class="mt-2">
                <h3>每月重复 - 阳历</h3>
                <div class="list-group">
                    <?php
                    foreach ($dayOfMonthsWithTask as $dayOfMonth) {

                        if ($dayOfMonth < 10) {
                            $dayOfMonth = "0" . $dayOfMonth;
                        }

                        echo <<<BBB
                            <a href="../edit.php?from=monthly&dayOfMonth={$dayOfMonth}" class="list-group-item list-group-item-action">每月 - 阳历 - {$dayOfMonth}号</a>
                        BBB;

                    }
                    ?>
                </div>
            </div>

        <?php endif; ?>

        <?php if (count($dayOfMonthsLunarWithTask) !== 0): ?>

            <div class="mt-2">
                <h3>每月重复 - 农历</h3>
                <div class="list-group">
                    <?php
                    foreach ($dayOfMonthsLunarWithTask as $dayOfMonth) {

                        $dayOfMonthChinese = $calendar->toChinaDay($dayOfMonth);

                        echo <<<BBB
                            <a href="../edit.php?from=monthlyLunar&dayOfMonth={$dayOfMonth}" class="list-group-item list-group-item-action">每月 - 农历 - {$dayOfMonthChinese}</a>
                        BBB;

                    }
                    ?>
                </div>
            </div>

        <?php endif; ?>

        <?php if (count($monthAndDaysWithTask) !== 0): ?>

            <div class="mt-2">
                <h3>每年重复 - 阳历</h3>
                <div class="list-group">
                    <?php
                    foreach ($monthAndDaysWithTask as $monthAndDay) {
                        $month = substr($monthAndDay, -4, 2); // 形如：02
                        $day = substr($monthAndDay, -2, 2); // 形如：03
                        echo <<<BBB
                            <a href="../edit.php?from=yearly&monthAndDay={$monthAndDay}" class="list-group-item list-group-item-action">每年 - 阳历 - {$month}月{$day}号</a>
                        BBB;
                    }
                    ?>
                </div>
            </div>

        <?php endif; ?>

        <?php if (count($monthAndDaysLunarWithTask) !== 0): ?>

            <div class="mt-2">
                <h3>每年重复 - 农历</h3>
                <div class="list-group">
                    <?php
                    foreach ($monthAndDaysLunarWithTask as $monthAndDay) {

                        $isLeap = "";
                        if (strlen($monthAndDay) == 5) {
                            $isLeap = "闰";
                        }

                        $month = substr($monthAndDay, -4, 2); // 形如：02
                        $monthChinese = $calendar->toChinaMonth($month); // 形如：二月

                        $day = substr($monthAndDay, -2, 2); // 形如：03
                        $dayChinese = $calendar->toChinaDay($day); // 形如：初三

                        $monthAndDayChinese = $isLeap . $monthChinese . $dayChinese; // 形如：闰二月初三

                        echo <<<BBB
                            <a href="../edit.php?from=yearlyLunar&monthAndDay={$monthAndDay}" class="list-group-item list-group-item-action">每年 - 农历 - {$monthAndDayChinese}</a>
                        BBB;

                    }
                    ?>
                </div>
            </div>

        <?php endif; ?>

    </div>

</div>

<!-- ----------------------------------------------------------------------- -->
<!--添加返回顶部按钮-->
<script src="../resources/vanilla-back-to-top/index.js"></script>
<script src="../resources/vanilla-back-to-top/config.js"></script>
<!-- ----------------------------------------------------------------------- -->

</body>
</html>
